package com.diyunapp.xilinapp.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.StoreListBean;
import com.diyunapp.xilinapp.myapi.OnRecyclerViewItemClickListener;
import com.diyunapp.xilinapp.utils.FormatConfirm;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by NIDAYE on 2016/6/15.
 */
public class RecycleViewStoreListtAdapter extends RecyclerView.Adapter<RecycleViewStoreListtAdapter.MyViewHolder> implements View.OnClickListener{
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;

    private Context myContext;
    public List<StoreListBean> mDatas;
    private View headView;
    private View footView;
    private int headViewSize = 0;
    private int footViewSize = 0;
    private ChangeGridLayoutManagerSpance changeGridLayoutManager;
    private boolean isAddFoot=false;
    private boolean isAddHead=false;

    //定义实现onItemClickClickListener 的接口
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public interface ChangeGridLayoutManagerSpance{
        public void change(int size, boolean isAddHead, boolean isAddFoot);
    }
    //提供接口给 让LayoutManager根据添加尾部 头部与否来做判断 显示头部与底部的SpanSize要在添加头部和尾部之后
    public void setChangeGridLayoutManager(ChangeGridLayoutManagerSpance changeGridLayoutManager){
        this.changeGridLayoutManager=changeGridLayoutManager;
        changeGridLayoutManager.change(getItemCount()-1,isAddHead,isAddFoot);
    }


    public RecycleViewStoreListtAdapter(Context myContext, List<StoreListBean> mDatas) {
        this.myContext = myContext;
        this.mDatas = mDatas;
    }

    public void addHeadView(View view) {
        headView = view;
        headViewSize = 1;
        isAddHead=true;
    }

    public void addFootView(View view) {
        footView = view;
        footViewSize = 1;
        isAddFoot=true;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;

        if (headViewSize==1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize==1 && position == getItemCount()-1) {
            //最后一个位置
            type = TYPE_FOOT;
        }
        return type;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        Log.i("zhangxiaoxidebug","MyViewHolder onCreateViewHolder");
        View view = null;
        switch (i) {
            case TYPE_HEADER:
                view = headView;
                break;

            case TYPE_ITEM:
                view = View.inflate(viewGroup.getContext(), R.layout.store_list_layout, null);
                view.setOnClickListener(this);
                break;

            case TYPE_FOOT:
                view =footView;
                break;
        }
        return new MyViewHolder(view,i);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(getItemViewType(position)==TYPE_ITEM)
        {
            int dataPosition = position - headViewSize;
            StoreListBean currentdata = mDatas.get(dataPosition);
            Log.i("zhangxiaoxidebug","currentdata = "+currentdata);
            holder.storeName_tv.setText(currentdata.getStoreName());
            String infoStr = "销量"+currentdata.getSaleNumbers() + "    共"+currentdata.getTotalgoods()+"宝贝";
            holder.storeInfo_tv.setText(infoStr);
            //设置信用度
            int creditNum = currentdata.getCreditValue();
            LinearLayout.LayoutParams myParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            myParams.setMargins(0, 0, 3, 0);
            for (int i = 0; i < creditNum; i++) {
                ImageView creditHeart = new ImageView(myContext);
                creditHeart.setImageResource(R.drawable.store_xin);
                holder.storecredit_ll.addView(creditHeart, myParams);
            }
            ImageLoader.getInstance().displayImage(currentdata.getStoreImage(),holder.store_img_iv, FormatConfirm.getOption());
            //将数据保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(dataPosition);
        }
    }

    @Override
    public int getItemCount()
    {
        int size = mDatas.size() + headViewSize + footViewSize;
        return size;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView store_img_iv;
        TextView storeName_tv;
        TextView storeInfo_tv;
        LinearLayout storecredit_ll;
        LinearLayout gotostore_ll;

        public MyViewHolder(View itemView,int type) {
            super(itemView);
            //当View是itemview时再执行此初始化操作
            if(type == TYPE_ITEM )
            {
                store_img_iv = (ImageView)itemView.findViewById(R.id.storelist_theimg_iv);
                storeName_tv =(TextView)itemView.findViewById(R.id.storelist_thename_tv);
                storeInfo_tv =(TextView)itemView.findViewById(R.id.storelist_googs_tv);
                storecredit_ll =(LinearLayout) itemView.findViewById(R.id.storelist_credit_ll);
                gotostore_ll =(LinearLayout) itemView.findViewById(R.id.goto_store_ll);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}



